<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/22/022
 * Time: 11:47
 */

namespace App\Http\Common\Lib;

class Time
{
	/**
	 * 某一天的开始与结束
	 * @param null $time
	 * @return array
	 */
	public static function day($time = null)
	{
		if (!$time) {
			$time = time();
		}
		$date = date('Y-m-d 00:00:00', $time);

		$start = strtotime($date);
		$end = $start + 3600 * 24 - 1;
		return [
			'start' => $start,
			'end' => $end,
		];
	}

	/**
	 * 后几天
	 * @param null $time
	 * @param int $days
	 * @return array
	 */
	public static function nextDays($time = null, $days = 1)
	{
		if (!$time) {
			$time = time();
		}
		$days = intval($days);
		$days = $days < 1 ? 1 : $days;
		$newTime = $time + 24 * 3600 * $days;
		return self::day($newTime);
	}

	/**
	 * 前几天
	 * @param null $time
	 * @param int $days
	 * @return array
	 */
	public static function lastDays($time = null, $days = 1)
	{
		if (!$time) {
			$time = time();
		}
		$days = intval($days);
		$days = $days < 1 ? 1 : $days;
		$newTime = $time - 24 * 3600 * $days;
		return self::day($newTime);
	}

	/**
	 * 某月的第一天与最后一天
	 * @param null $time
	 * @return array
	 */
	public static function month($time = null)
	{
		if (!$time) {
			$time = time();
		}
		$days = date('t', $time);
		$year = date('Y', $time);
		$month = date('m', $time);
		return [
			'start' => strtotime($year . '-' . $month . '-01 00:00:00'),
			'end' => strtotime($year . '-' . $month . '-' . $days . ' 23:59:59'),
		];
	}

	/**
	 * 下个月
	 * @param null $time
	 * @return array
	 */
	public static function nextMonth($time = null)
	{
		if (!$time) {
			$time = time();
		}
		$days = date('t', $time);
		$year = date('Y', $time);
		$month = date('m', $time);
		$newTime = strtotime($year . '-' . $month . '-' . $days . ' 23:59:59') + 2;
		return self::month($newTime);
	}

	/**
	 * 上个月
	 * @param null $time
	 * @return array
	 */
	public static function lastMonth($time = null)
	{
		if (!$time) {
			$time = time();
		}
		$year = date('Y', $time);
		$month = date('m', $time);
		$newTime = strtotime($year . '-' . $month . '-01 00:00:00') - 2;
		return self::month($newTime);
	}

	/**
	 * 下个月的同一天
	 * @param null $time
	 * @return false|int
	 */
	public static function nextMonthDay($time = null)
	{
		if (!$time) {
			$time = time();
		}
		$day = date('d', $time);
		$hour = date('H', $time);
		$minute = date('i', $time);
		$second = date('s', $time);
		$nextMonth = self::nextMonth($time);
		$nextMonthDays = date('t', $nextMonth['start']);
		$day = $day > $nextMonthDays ? $nextMonthDays : $day;
		$nextDate = date('Y-m', $nextMonth['start']) . '-' . $day . ' ' . $hour . ':' . $minute . ':' . $second;
		return strtotime($nextDate);
	}

	/**
	 * 上个月的同一天
	 * @param null $time
	 * @return false|int
	 */
	public static function lastMonthDay($time = null)
	{
		if (!$time) {
			$time = time();
		}
		$day = date('d', $time);
		$hour = date('H', $time);
		$minute = date('i', $time);
		$second = date('s', $time);
		$nextMonth = self::lastMonth($time);
		$nextMonthDays = date('t', $nextMonth['start']);
		$day = $day > $nextMonthDays ? $nextMonthDays : $day;
		$nextDate = date('Y-m', $nextMonth['start']) . '-' . $day . ' ' . $hour . ':' . $minute . ':' . $second;
		return strtotime($nextDate);
	}


}